/*
 * Created on 2006-4-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.shipment;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Download {
    
    public static Download download = new Download();
    
    public Download(){
        init();
    }
    
    public static Download getInstance(){
        return download; 
    }
    
    public void setDownloadPath(String path){
        
    }
    
    /**
     * 初始化文件下载的本地缓存路径
     *
     */
    public static void init(){
        
        try{
	        File pathFile = new File(GSSystemConfigurator.getParam("gs.auth.savepath"));
	        if(!pathFile.exists()){
	            pathFile.mkdir();
	        }
	        String yesterday = DateUtil.getYesterDay().replace('/','-');
	        
        }catch(Exception e){
            //GSLogWriter.fatal(GSException.E17778,GSException.E17778_MSG);
        }

    }

    /**
     * 传输订单明细对帐文件
     * @param date
     * @throws GSException
     */
    public void transactionShipmentFile(String date) throws GSException {
        String fileName = GSShipmentConstants.getFileNameSimple(date);
        trans(fileName);
    }
    
    
    /**
     * 具体的文件下载执行器
     * @param fileName 要下载的文件名，带url
     * @throws GSException
     */
    private void trans(String fileName) throws GSException {
        FileInputStream fis = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        try{
            URL url = new URL(GSSystemConfigurator.getParam("gs.auth.schema") + "://" + GSSystemConfigurator.getParam("gs.auth.host") + ":" + GSSystemConfigurator.getParam("gs.auth.port") + "/" + GSSystemConfigurator.getParam("gs.auth.fullpath") + "/" + fileName);
            java.io.File f2 = new File(GSSystemConfigurator.getParam("gs.auth.savepath") + System.getProperty("file.separator") + fileName);

            if(!f2.exists()) {
                dis = new DataInputStream(url.openStream());
                f2.createNewFile();
	            fos = new FileOutputStream(f2);
	            
	            byte[] buffer = new byte[512];
	            int length = 0;
	            while((length = dis.read(buffer,0,512))!=-1){
	                fos.write(buffer,0,length);
	            }
	            
	            fos.flush();
	            fos.close();
	            dis.close();
            }
            
        }
        catch(IOException ioe){
            ioe.printStackTrace();
            throw new GSException (GSException.E17780,GSException.E17780_MSG);
        }
        
    }
}
